package com.itheima.controller.admin;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Api(tags = "检查组相关接口")
@Slf4j
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 新增检查组
     * @param group,checkitemIds
     * @return
     */
    @ApiOperation("新增检查组")
    @PostMapping("/add")
    public Result addCheckGroup(@RequestBody CheckGroup group,String checkitemIds){
        log.info("新增检查组:{}",group);
        List<Integer> list = ids(checkitemIds);
        checkGroupService.addCheckGroup(group,list);
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS,null);
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @ApiOperation("根据id删除检查组")
    @GetMapping("/deleteById")
    public Result deleteCheckGroupById(Integer id){
        log.info("根据id删除检查组:{}",id);
        checkGroupService.deleteById(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS,null);
    }

    /**
     * 编辑检查组
     * @param group
     * @param checkitemIds
     * @return
     */
    @ApiOperation("编辑检查组")
    @PostMapping("/edit")
    public Result editCheckGroup(@RequestBody CheckGroup group,String checkitemIds){
        log.info("编辑检查组:{},{}",group,checkitemIds);
        List<Integer> list = ids(checkitemIds);
        checkGroupService.editCheckGroup(group,list);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS,null);
    }

    /**
     * 查询全部检查组
     * @return
     */
    @GetMapping("/findAll")
    @ApiOperation("查询全部检查组")
    public Result findAll(){
        log.info("查询全部检查组");
        List<CheckGroup> list = checkGroupService.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    /**
     * 分页查询检查项
     * @param pageBean
     * @return
     */
    @PostMapping("findPage")
    @ApiOperation("分页查询检查项")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        log.info("分页查询检查项:{}",pageBean);
        PageResult result = checkGroupService.findPage(pageBean);
        return result;
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询检查组")
    public Result findAll(Integer id){
        log.info("根据id查询检查组:{}",id);
        CheckGroup result= checkGroupService.findById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,result);
    }

    /**
     * 根据套餐id查询检查组id
     * @param id
     * @return
     */
    @ApiOperation("根据套餐id查询检查组id")
    @GetMapping("findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer setmealId){
        log.info("根据套餐id查询检查组:{}",setmealId);
        List<Integer> list = checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    /**
     * 将ids处理为List集合
     * @param checkitemIds
     * @return
     */
    public List<Integer> ids(String checkitemIds){

        String[] split = checkitemIds.split(",");
        List<Integer> list = new ArrayList<>();
        for (String s : split) {
            list.add(Integer.valueOf(s));
        }
        return list;
    }
}
